<?php

namespace App\Http\Middleware;

use Closure;
use App\AdminGlobals;
use App\Api\Exceptions\ApiCommonException;

class AdminAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $user = auth()->guard('admin')->user();
        if (empty($user)) {
            throw ApiCommonException::loginInvalidCredentials();
        }

        AdminGlobals::$admin_id = $user->id;

        return $next($request);
    }
}